package com.guli.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.guli.commonUtils.R;
import com.guli.vod.utils.AliyuVodClientUtil;
import com.guli.vod.utils.ConstantPropertiesGet;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

/**
 * @author zxc
 * @date 2023/04/25 20:23
 **/
@Api(description = "整合阿里云视频在线播放功能")
@RestController
@RequestMapping("/edu/videoVod")
//@CrossOrigin // 解决跨域问题
public class VideoPlayController {

    @ApiOperation(value = "获取所要播放视频的凭证")
    @GetMapping("/playVideo/{videoId}")
    public R getPlayAuth(
            @ApiParam(name = "videoId", value = "视频id", required = true)
            @PathVariable("videoId") String videoId) throws ClientException {
        System.out.println(videoId);

        String accessKeyId = ConstantPropertiesGet.KEY_ID;
        String accessKeySecret = ConstantPropertiesGet.KEY_SECRET;
        // 根据视频id，来获取播放视频播放凭证
        // 加密视频：获取到音视频地址是不能够进行播放的，同时也需要获取该加密视频的凭证信息;
        // 1.创建初始化HttpClient对象
        DefaultAcsClient client = AliyuVodClientUtil.initVodClient(accessKeyId, accessKeySecret);

        // 2.获取视频凭证的request和response
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();

        // 3.设置视频id
        request.setVideoId(videoId);

        // 4.调用client对象中所封装的方法，以request作为参数进行传递，得到音视频凭证;
        response = client.getAcsResponse(request);

        String playAuth = response.getPlayAuth();

        return R.ok().data("playAuth", playAuth);
    }
}
